package com.shujia.flink.tf;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo4KeyBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> words = env.socketTextStream("master", 8888);

        //匿名内部类
        words.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String word) throws Exception {
                return word;
            }
        });

        //lambda表达式
        //keyBy:将相同的key发送到同一个task中
        words.keyBy(word -> word).print();

        env.execute();
    }
}
